package cn.itheima.student.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.itheima.student.daomain.admin;
import cn.itheima.student.service.LoginService;
import cn.itheima.student.service.impl.LoginServceImpl;


public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id = request.getParameter("adminname");
		String epss = request.getParameter("password");
		
		LoginService service = new LoginServceImpl();
		admin a =service.Login(id, epss);
		
		if (a != null) {
			request.getRequestDispatcher("index.jsp").forward(request, response);
			HttpSession session = request.getSession();
			session.setAttribute("admin", a);
		}else {
			PrintWriter out = response.getWriter();
			out.print("<script language='javascript'>alert('登录失败！请从新登陆');window.location.href='Login.jsp';</script>");
		}
	
	
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
